<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Start new or resume existing session</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.session-set-save-handler.html">session_set_save_handler</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.session-status.html">session_status</a></div>
 <div class="up"><a href="ref.session.html">Session 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.session-start" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">session_start</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">session_start</span> &mdash; <span class="dc-title">Start new or resume existing session</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.session-start-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>session_start</strong></span>
    ( <span class="methodparam">void</span>
   )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>session_start()</strong></span> creates a session or resumes the
   current one based on a session identifier passed via a GET or POST
   request, or passed via a cookie.
  </p>
  <p class="para">
   When <span class="function"><strong>session_start()</strong></span> is called or when a session auto starts,
   PHP will call the open and read session save handlers.  These will either be a built-in
   save handler provided by default or by PHP extensions (such as SQLite or Memcached); or can be
   custom handler as defined by <span class="function"><a href="function.session-set-save-handler.html" class="function">session_set_save_handler()</a></span>.
   The read callback will retrieve any existing session data (stored in a special serialized format)
   and will be unserialized and used to automatically populate the $_SESSION superglobal when the
   read callback returns the saved session data back to PHP session handling.
  </p>
  <p class="para">
   To use a named session, call
   <span class="function"><a href="function.session-name.html" class="function">session_name()</a></span> before calling
   <span class="function"><strong>session_start()</strong></span>.
  </p>
  <p class="para">
   When <a href="session.configuration.html#ini.session.use-trans-sid" class="link">session.use_trans_sid</a>
   is enabled, the <span class="function"><strong>session_start()</strong></span> function will
   register an internal output handler for URL rewriting.
  </p>
  <p class="para">
   If a user uses <em>ob_gzhandler</em> or similar with
   <span class="function"><a href="function.ob-start.html" class="function">ob_start()</a></span>, the function order is important for
   proper output.  For example,
   <em>ob_gzhandler</em> must be registered before starting the session.
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-start-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   This function returns <strong><code>TRUE</code></strong> if a session was successfully started,
   otherwise <strong><code>FALSE</code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.session-start-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        If a session fails to start, then <strong><code>FALSE</code></strong> is returned.
        Previously <strong><code>TRUE</code></strong> was returned.
       </td>
      </tr>

      <tr>
       <td>4.3.3</td>
       <td>
        As of PHP 4.3.3, calling <span class="function"><strong>session_start()</strong></span>
        after the session was previously started will result in an
        error of level <strong><code>E_NOTICE</code></strong>.  Also, the
        second session start will simply be ignored.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.session-start-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4674">
    <p><strong>Example #1 A session example: <var class="filename">page1.php</var></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;page1.php<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #DD0000">'Welcome&nbsp;to&nbsp;page&nbsp;#1'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'favcolor'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'animal'</span><span style="color: #007700">]&nbsp;&nbsp;&nbsp;=&nbsp;</span><span style="color: #DD0000">'cat'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'time'</span><span style="color: #007700">]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Works&nbsp;if&nbsp;session&nbsp;cookie&nbsp;was&nbsp;accepted<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;&lt;a&nbsp;href="page2.php"&gt;page&nbsp;2&lt;/a&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Or&nbsp;maybe&nbsp;pass&nbsp;along&nbsp;the&nbsp;session&nbsp;id,&nbsp;if&nbsp;needed<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;&lt;a&nbsp;href="page2.php?'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">SID&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'"&gt;page&nbsp;2&lt;/a&gt;'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   After viewing <var class="filename">page1.php</var>, the second page
   <var class="filename">page2.php</var> will magically contain the session
   data.  Read the <a href="ref.session.html" class="link">session reference</a>
   for information on <a href="session.idpassing.html" class="link">propagating
   session ids</a> as it, for example, explains what the constant
   <strong><code>SID</code></strong> is all about.
  </p>
  <p class="para">
   <div class="example" id="example-4675">
    <p><strong>Example #2 A session example: <var class="filename">page2.php</var></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;page2.php<br /><br /></span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #DD0000">'Welcome&nbsp;to&nbsp;page&nbsp;#2&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'favcolor'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;green<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'animal'</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;cat<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y&nbsp;m&nbsp;d&nbsp;H:i:s'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">'time'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;You&nbsp;may&nbsp;want&nbsp;to&nbsp;use&nbsp;SID&nbsp;here,&nbsp;like&nbsp;we&nbsp;did&nbsp;in&nbsp;page1.php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;&lt;a&nbsp;href="page1.php"&gt;page&nbsp;1&lt;/a&gt;'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.session-start-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    To use cookie-based sessions, <span class="function"><strong>session_start()</strong></span>
    must be called before outputing anything to the browser.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Use of <a href="zlib.configuration.html#ini.zlib.output-compression" class="link">zlib.output_compression</a>
    is recommended instead of <span class="function"><a href="function.ob-gzhandler.html" class="function">ob_gzhandler()</a></span>
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This function sends out several HTTP headers depending on the
    configuration. See <span class="function"><a href="function.session-cache-limiter.html" class="function">session_cache_limiter()</a></span> to
    customize these headers.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-start-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var></li>
    <li class="member">
     The <a href="session.configuration.html#ini.session.auto-start" class="link">session.auto_start</a>
     configuration directive
    </li>
    <li class="member"><span class="function"><a href="function.session-id.html" class="function" rel="rdfs-seeAlso">session_id()</a> - Get and/or set the current session id</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.session-set-save-handler.html">session_set_save_handler</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.session-status.html">session_status</a></div>
 <div class="up"><a href="ref.session.html">Session 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
